package com.tencent.system.controller;

import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.PageList;
import com.tencent.system.domain.SystemDictionaryDetail;
import com.tencent.system.query.SystemDictionaryDetailQuery;
import com.tencent.system.service.ISystemDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后端接口 ： 数据字典的controller
 */
@RestController
@RequestMapping("/dictionaryDetail")
public class SystemDictionaryDetailController {

    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    @GetMapping
    public List<SystemDictionaryDetail> queryAll() {
        return systemDictionaryDetailService.queryAll();
    }

    @GetMapping("/{id}")
    public SystemDictionaryDetail queryById(@PathVariable("id") Long id) {
        return systemDictionaryDetailService.queryById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        try {
            if(systemDictionaryDetail.getId() == null) {
                systemDictionaryDetailService.save(systemDictionaryDetail);
            } else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙...").setSuccess(false);
        }

    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemDictionaryDetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙...").setSuccess(false);
        }
    }

    @PostMapping
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery query) {
        return systemDictionaryDetailService.queryPage(query);
    }

    @PatchMapping
    // 前端传递过来的数组 我们既可以使用数组接收 也可以使用集合List<Long> 接收
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            systemDictionaryDetailService.patchDelete(ids);
            return AjaxResult.me(); // 返回一个AjaxResult对象 默认是true ，操作成功
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！"); // 同一个AjaxResult对象 链式编程
        }
    }


}
